<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'edit','title'=>'新建商品分类'),	
);
//商品分类
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0,'mid'=>0);
	$params['parentid']=intval($_RQ['id']);
	$return['params']['parentid'] = $params['parentid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['id'])){
		$return['params']['parentid']=intval($_RQ['id']);
	}
	if(!empty($_RQ['parentid'])){
		$params['parentid']=intval($_RQ['parentid']);
	}
	$params['onepluginname']='';
	if($_SESSION['current_plugin']!='jy_weishop'){
		$pluginttitle=pdo_getcolumn('core_plugins',array('name'=>$_SESSION['current_plugin']),'title');	
	}
	$parenttitle=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$params['parentid']),'title');
	$parenttitle=$parenttitle?$parenttitle.'子':'';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','parentid','onepluginname','pluginname','uniacid','title','isshow','isrecommand','image','paixu');
	$list=pdo_getslice('jy_weishop_goods_group',$params,array($pindex,$psize),$total,$fields,'','paixu desc, id desc');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'一级分类列表'),
			array('op'=>'edit','title'=>'添加分类','query'=>'parentid='.$params['parentid']),
		),
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'image','title'=>'分类图片','type'=>'image'),
			array('name'=>'title','title'=>'分类名称','type'=>'string'),
		),'action'=>array(
			array('type'=>'href','op'=>'list2','title'=>'二级菜单管理','show'=>array('parentid'=>0)),
			array('type'=>'href','op'=>'edit','title'=>'翻译'),
		)),
		'search_one'=>array('name'=>'title','title'=>'商品分类名称','add'=>false),
	);
	$return['htmls']=$htmls;
	$status=array('0'=>'显示','1'=>'隐藏');
	$return['status']=$status;
	exi($return);
}
//商品分类
if($_RQ['op']=='list2'){
	$shopset=$this->plugin_setting('shopset');
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0);
	$params['parentid']=intval($_RQ['id'])?:intval($_RQ['parentid']);
	$return['params']['parentid'] = $params['parentid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$parenttitle=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$params['parentid']),'title');
	$parenttitle=$parenttitle?'【'.$parenttitle.'】子':'';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','parentid','onepluginname','pluginname','uniacid','title','isshow','isrecommand','image','paixu');
	$list=pdo_getslice('jy_weishop_goods_group',$params,array($pindex,$psize),$total,$fields,'','paixu desc, id desc');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'一级分类'),
			array('op'=>'list2','title'=>$parenttitle.'分类列表'),
			array('op'=>'edit','title'=>'添加分类','query'=>'parentid='.$params['parentid']),
		),
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'image','title'=>'分类图片','type'=>'image'),
			array('name'=>'title','title'=>'分类名称','type'=>'string'),
		),'action'=>array(
			array('type'=>'href','op'=>'list3','title'=>'三级菜单管理','show'=>array('grouplevel'=>1)),
			array('type'=>'href','op'=>'edit','title'=>'翻译'),
		)),
		'search_one'=>array('name'=>'title','title'=>'商品分类名称','add'=>false),
	);
	$return['htmls']=$htmls;
	$status=array('0'=>'显示','1'=>'隐藏');
	$return['status']=$status;
	exi($return);
}
if($_RQ['op']=='list3'){
	$shopset=$this->plugin_setting('shopset');
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0);
	$params['parentid']=intval($_RQ['id'])?:intval($_RQ['parentid']);
	$return['params']['parentid'] = $params['parentid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$parenttitle=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$params['parentid']),'title');
	$parenttitle=$parenttitle?'【'.$parenttitle.'】子':'';
	$parentid2=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$params['parentid']),'parentid');
	$parenttitle2=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$parentid2),'title');
	$parenttitle2=$parenttitle2?'【'.$parenttitle2.'】子':'';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','parentid','onepluginname','pluginname','uniacid','title','isshow','isrecommand','image','paixu');
	$list=pdo_getslice('jy_weishop_goods_group',$params,array($pindex,$psize),$total,$fields,'','paixu desc, id desc');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'一级分类'),
			array('op'=>'list2','title'=>$parenttitle2.'分类','query'=>'parentid='.$parentid2),
			array('op'=>'list3','title'=>$parenttitle.'分类'),
			array('op'=>'edit','title'=>'添加分类','query'=>'parentid='.$params['parentid']),
		),
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'image','title'=>'分类图片','type'=>'image'),
			array('name'=>'title','title'=>'分类名称','type'=>'string'),
		),'action'=>array(
			array('type'=>'href','op'=>'edit','title'=>'翻译'),
		)),
		'search_one'=>array('name'=>'title','title'=>'商品分类名称','add'=>false),
	);
	$return['htmls']=$htmls;
	$status=array('0'=>'显示','1'=>'隐藏');
	$return['status']=$status;
	exi($return);
}

if($_RQ['op']=='edit'){
	$return=array();
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$isset=$this->pdo_get('goods_group',array('fromid'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$data=array(
			'title'=>$post['title'],
			'diy'=>ijson_encode($post['diy'])
		);
		if($isset){
			$this->pdo_update('goods_group',$data,array('fromid'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['fromid']=$id;
			$this->pdo_insert('goods_group',$data);
		}
		exi('设置成功');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_goods_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择分类','error');	
	}
	$isset=$this->pdo_get('goods_group',array('fromid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['diy']=json_decode($isset['diy'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'分类内容',
		'edit_tab'=>true,
		'edits'=>array(),
	);
	$list=array(
		array('name'=>'title','tip'=>'分类名称','type'=>'stringdiy'),
		array('name'=>'desc','tip'=>'分类描述','type'=>'stringdiy'),
	);
	$is_langs=str_array($this->setting['is_langs']);
	foreach($is_langs as $en){
		$one=array();
		$one['title']=$this->langs[$en];
		$one['list']=array();
		$one['list'][]=array('type'=>'jsup','title'=>'一键翻译本页内容','action'=>'jy_lang_weishop/index/upfanyi/en='.$en,'message'=>'确定要一键翻译吗？这样将覆盖原有设置','btntitle'=>'一键翻译');
		foreach($list as $fv){
			$fv['title']=$item[$fv['name']];
			$fv['name']='diy.'.$en.'_'.$fv['name'];
			$one['list'][]=$fv;
		}
		$htmls['edits'][]=$one;
	}
	if(!count($is_langs)){
		exi('基础设置--多语言开启--开启店铺语言（至少一个）,这里才显示内容','error');
	}
	$return['item']['fanyidata']=array();
	foreach($list as $k=>$fv){
		$return['item']['fanyidata']['_'.$fv['name']]=$fv['title'];
	}
	$return['htmls']=$htmls;
	exi($return);
}